﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Text.RegularExpressions;

namespace Hosting.Class
{
    public class About
    {

        public About() { }

        #region [Get all About]
        /// <summary>
        /// Get all About
        /// </summary>
        /// <param name="getActive">
        /// <para>0     : false</para>
        /// <para>1     : true</para>
        /// <para>-1    : get all</para>
        /// </param>
        /// <returns>
        /// Get one About
        /// Null    : error
        /// </returns>
        public List<PC_About_getAllResult> getAll(string getActive)
        {
            try
            {
                HostingDomainWebsiteDataContext db = new HostingDomainWebsiteDataContext();
                var list = db.PC_About_getAll(Int32.Parse(getActive)).ToList();
                return list;
            }
            catch (Exception ex)
            {
                return null;
            }

        }
        #endregion

        #region [Get one About]
        /// <summary>
        /// Get one About
        /// </summary>
        /// <param name="id">ID of About</param>
        /// <returns>Get one About</returns>
        /// <returns>null: error</returns>
        public List<PC_About_getOneResult> getOne(string id)
        {
            try
            {
                HostingDomainWebsiteDataContext db = new HostingDomainWebsiteDataContext();
                var list = db.PC_About_getOne(Int32.Parse(id)).ToList();
                return list;
            }
            catch (Exception ex)
            {
                return null;
            }

        }
        #endregion

        #region [Insert a new About]
        /// <summary>
        /// Insert a new About
        /// </summary>
        /// <param name="title">Title of About</param>
        /// <param name="content">Content of Company</param>
        /// <param name="active">Visible/Hidden About on interface</param>
        /// <returns>0: successfully</returns>
        /// <returns>-99: error</returns>
        public int add(string title, string content, bool active)
        {
            try
            {
                HostingDomainWebsiteDataContext db = new HostingDomainWebsiteDataContext();
                var isInsert = db.PC_About_Ins(title, content, active);
                return isInsert;
            }
            catch (Exception ex)
            {
                return -99;
            }
        }
        #endregion

        #region [Check insert About is valid or not]
        /// <summary>
        /// Check insert About is valid or not
        /// </summary>
        /// <param name="title">Title of About</param>
        /// <returns>1: successfully</returns>
        /// <returns>10: Title is null or empty</returns>
        /// <returns>11: Tittle is greater than 100</returns>
        public int addValid(string title)
        {
            if (String.IsNullOrEmpty(title))
            {
                return 10;
            }
            else if (title.Length > 100)
            {
                return 11;
            }

            return 1;
        }
        #endregion

        #region [Update a new About]
        /// <summary>
        /// Update a new About
        /// </summary>
        /// <param name="title">Title of About</param>
        /// <param name="content">Content of Company</param>
        /// <param name="active">Visible/Hidden About on interface</param>
        /// <returns>0: successfully</returns>
        /// <returns>-99: error</returns>
        public int edit(string id, string title, string content, bool active)
        {
            try
            {
                HostingDomainWebsiteDataContext db = new HostingDomainWebsiteDataContext();
                var isInsert = db.PC_About_Udp(Int32.Parse(id), title, content, active);
                return isInsert;
            }
            catch (Exception ex)
            {
                return -99;
            }
        }
        #endregion

        #region [Check update About is valid or not]
        /// <summary>
        /// Check update About is valid or not
        /// </summary>
        /// <param name="id">ID of About</param>
        /// <param name="title">Title of About</param>
        /// <returns>1: successfully</returns>
        /// <returns>2: ID About is null or empty</returns>
        /// <returns>3: ID About is not valid</returns>
        /// <returns>10: Title is null or empty</returns>
        /// <returns>11: Tittle is greater than 100</returns>
        public int editValid(string id, string title)
        {
            if (String.IsNullOrEmpty(id))
            {
                return 2;
            }

            Regex rId = new Regex("^[0-9]{1,}$");
            Match mId = rId.Match(id);
            if (!mId.Success)
            {
                return 3;
            }
            else if (String.IsNullOrEmpty(title))
            {
                return 10;
            }
            else if (title.Length > 100)
            {
                return 11;
            }

            return 1;
        }
        #endregion

        #region [Delete an existed About]
        /// <summary>
        /// Delete an existed About
        /// </summary>
        /// <param name="id">ID of an about</param>
        /// <returns>
        /// <para>0         : successfully</para>
        /// <para>-99       : error</para>
        /// </returns>
        public int delete(string id)
        {
            try
            {
                HostingDomainWebsiteDataContext db = new HostingDomainWebsiteDataContext();
                var isDelete = db.PC_About_Del(Int32.Parse(id));
                return isDelete;
            }
            catch(Exception ex)
            {
                return -99;
            }
        }
        #endregion

    }
}